/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2013 All Rights Reserved.
 */
package com.pingan.mobilegw.core.service.business;
import com.pingan.mobilegw.core.model.GatewayRequest;
import com.pingan.mobilegw.core.model.GatewayResponse;
import com.pingan.mobilegw.core.model.response.ResponseBody;
import com.pingan.mobilegw.core.model.response.SingleResponseBody;
import com.pingan.mobilegw.core.service.support.groovy.GroovyProcessor;
import com.pingan.mobilegw.core.service.support.groovy.GroovyScriptFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.Collection;

/**
 * 具有处理Groovy脚本功能的Processor包裹类
 * 
 * @author jiadao
 * @version $Id: GroovyProcessorWrapper.java, v 0.1 2013-7-15 下午1:33:02 jiadao Exp $
 */
public class GroovyProcessorWrapper implements Processor {

    private static final Log logger = LogFactory.getLog(GroovyProcessorWrapper.class);

    /** 原始Processor */
    private Processor           processor;

    /**
     * 默认构造函数
     * 
     * @param processor
     */
    public GroovyProcessorWrapper(Processor processor) {
        this.processor = processor;
    }

    @Override
    public GatewayResponse process(GatewayRequest gatewayRequest) {
        //TODO
        if(2==1) {
            GatewayResponse newResponse = new GatewayResponse();
            ResponseBody responseBody = new SingleResponseBody(){
                @Override
                public Object getResult() {
                    return "Hello!Alex";
                }

                @Override
                public int getResultStatus() {
                    return 1;
                }

                @Override
                public String getMemo() {
                    return null;
                }

                @Override
                public String getTips() {
                    return null;
                }
            };
            newResponse.setResponseBody(responseBody);
            return newResponse;
        }
        // 处理Groovy脚本
        if(false) {
            Collection<GroovyProcessor> groovyProcessors = GroovyScriptFactory.getGroovyProcessors();
            if (!CollectionUtils.isEmpty(groovyProcessors)) {

                for (GroovyProcessor groovyProcessor : groovyProcessors) {
                    try {
                        // 判断是否需要执行
                        if (groovyProcessor.needProcess(gatewayRequest)) {
                            GatewayResponse response = groovyProcessor.process(gatewayRequest);
                            // 返回结果不为null，则直接返回
                            if (response != null && response.getResponseBody() != null) {
                                return response;
                            }
                        }

                    } catch (Exception e) {
                        logger.error("执行groovy脚本失败！", e);
                    }
                }
            }
        }
        // 正常处理请求
        return processor.process(gatewayRequest);

        //后置Groovy脚本处理 TODO
    }

}
